﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using kysec.RAL.EFToMySql;
using System.Data.Linq.SqlClient;

namespace kysec_MVC.Controllers
{
    public class MessageController : Controller
    {
        #region 后台操作显示
        /// <summary>
        /// 实现分页显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult InfoList(int? id, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("Message_{0}_{1}", id, type + DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    var orders = (from c in db.tb_message
                                  where c.c_i_typeId == type
                                  orderby c.c_dt_publishedTime descending
                                  select c).ToPagedList(id ?? 1, 10); 
                    
                    if (Request.IsAjaxRequest())
                        return PartialView("UCMessage", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("Message_{0}_{1}", id, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }

        /// <summary>
        /// 查找出具体未回复的信息
        /// </summary>
        public ActionResult SearchMsg(string id,string type)
        {             
            List<kysec.RAL.EFToMySql.tb_message> listMsg = kysec.Bll.Message.Search.getMsg(id, int.Parse(type));//已回复              

            return View(listMsg);
        }

        /// <summary>
        /// 查找出具体已回复的信息
        /// </summary>
        public ActionResult SearchReplayMsg(string id, string type)
        {
            List<kysec.RAL.EFToMySql.vw_msg_repmsg> list = kysec.Bll.Message.vw_msg_replay.getMsaage(id, int.Parse(type));//没有回复
            
            return View(list);            
            
        }



        /// <summary>
        /// 添加回复信息
        /// </summary>
        [HttpPost]
        public string AddRepalyMsg(string id,kysec.RAL.EFToMySql.tb_message_replay model)
        {
            string result = "0";
            
            result=kysec.Bll.Message.msg_replay.UpdateReplayMsg(id,model);

            return result;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        public void Delete(string id,string type)
        {
            string result = "0";

            result = kysec.Bll.Message.message.DeleteMessage(id);

            if (result.Equals("1"))
            {
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Message/InfoList?type=" + type);               
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        public string DeleteOne(string id)
        {
            string result = "0";

            result = kysec.Bll.Message.message.DeleteMessage(id);

            return result;
        }
        #endregion 

        #region 前台页面显示
        ///// <summary>
        ///// 添加留言信息
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult Online_Msg()
        //{          
        //    return View();
        //}
        /// <summary>
        /// 留言信息页面显示，RSS格式
        /// </summary>
        /// <returns></returns>
        public ActionResult Online_Msg(int? id, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(string.Format("MsgList_{0}_{1}",id,type+DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    var orders = (from c in db.vw_msg_repmsg
                              where c.msgDel == false && c.c_i_typeId == type
                              orderby c.c_dt_publishedTime descending
                              select c).ToPagedList(id ?? 1,10);

                    if (Request.IsAjaxRequest())
                        return PartialView("ucMsgList", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("MsgList_{0}_{1}", id, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }
        /// <summary>
        /// 客户留言
        /// </summary>
        [HttpPost]
        public string AddMsg(string type,kysec.RAL.EFToMySql.tb_message model)
        {
            string result = "0";

            
            result = kysec.Bll.Message.message.AddMessage(int.Parse(type), model);

            //if (result.Equals("1"))
            //{
            //    Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Message/MsgList?type=1");
            //}
            return result;
        }
        /// <summary>
        /// 技术反馈
        /// </summary>
        /// <returns></returns>
        public ActionResult Technical_Feedback()
        {
            return View();
        }
        /// <summary>
        /// 技术反馈信息显示，RSS格式
        /// </summary>
        /// <returns></returns>
        public ActionResult TechnicalList(int?id, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(string.Format("TechnicalList_{0}_{1}", id, type + DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    var orders = (from c in db.vw_msg_repmsg
                                  where c.msgDel == false && c.c_i_typeId == type
                                  orderby c.c_dt_publishedTime descending
                                  select c).ToPagedList(id ?? 1, 10);

                    if (Request.IsAjaxRequest())
                        return PartialView("ucMsgList", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("TechnicalList_{0}_{1}", id, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }
        #endregion
    }
}
